pub trait OutputStream {
    fn write_from(&mut self, data:&[u8]) -> OutputResult<usize>;
    fn write_rle_byte(&mut self, byte:u8, times:usize) -> OutputResult<usize>;
}

impl<O:std::io::Write> OutputStream for O {
    fn write_from(&mut self, data:&[u8]) -> OutputResult<usize> {
        Ok(self.write(data)?)
    }

    fn write_rle_byte(&mut self, byte:u8, times:usize) -> OutputResult<usize> {
        let data = [byte].repeat(times);
        Ok(self.write(&data)?)
    }
}

pub type OutputResult<T> = Result<T,OutputError>;

#[derive(Debug)]
pub enum OutputError {
    Io(std::io::Error),
}

impl std::fmt::Display for OutputError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io(e) => write!(f,"[byte][io]{e}"),
        }
    }
}

impl std::error::Error for OutputError { }

impl From<std::io::Error> for OutputError {
    fn from(value: std::io::Error) -> Self {
        Self::Io(value)
    }
}
